<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Pic;

class UploadController extends Controller
{
    public function image(Request $request){
        $file=$request->file('file');
        $type=$request->type;
        try{
            if(!is_array($file)){
                $pre=pathinfo($file->getClientOriginalName(),PATHINFO_EXTENSION);
                $data['size']=$file->getClientSize();
                $data['mime']=$file->getClientMimeType();
                $data['type']=$type;
                $re=Pic::create($data);
                $filename=md5($type.$re->id).".".$pre;
                $re->realname=$filename;
                $url="/upload/".$file->storeAs("/$type/".date('Ym',time()),$filename);
                $re->path=$url;
                $re->update();
                return parent::success('上传成功',$re);
            }else{
                print_r($file);
            }
        }catch (\Exception $e) {
            return parent::error('上传失败');
        }

    }
}
